package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmSmsReceiveRole;
import com.zb.mapper.ZjAdmSmsReceiveRoleMapper;
import com.zb.service.ZjAdmSmsReceiveRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台短信接收角色表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-22
 */
@Service
public class ZjAdmSmsReceiveRoleServiceImpl extends ServiceImpl<ZjAdmSmsReceiveRoleMapper, ZjAdmSmsReceiveRole> implements ZjAdmSmsReceiveRoleService {
    @Resource
    private ZjAdmSmsReceiveRoleMapper admSmsReceiveRoleMapper;

    // 保存短信接收角色
    @Override
    public void save(Long admSmsTemplateId, String code, List<Integer> roleTypes) {
        admSmsReceiveRoleMapper.deleteByAdmSmsTemplateId(admSmsTemplateId);
        if (CollectionUtil.isEmpty(roleTypes)) {
            return;
        }
        List<ZjAdmSmsReceiveRole> smsReceiveRoles = new ArrayList<>();
        ZjAdmSmsReceiveRole smsReceiveRole = null;
        for (Integer roleType : roleTypes) {
            smsReceiveRole = new ZjAdmSmsReceiveRole();
            smsReceiveRole.setId(IdWorker.getId());
            smsReceiveRole.setAdmSmsTemplateId(admSmsTemplateId);
            smsReceiveRole.setRoleType(roleType);
            smsReceiveRoles.add(smsReceiveRole);
        }
        this.saveBatch(smsReceiveRoles);
    }

    // 根据后台短信模板ID获取接收角色类型
    @Override
    public List<ZjAdmSmsReceiveRole> getByAdmSmsTemplateIds(List<Long> admSmsTemplateIds) {
        if (CollectionUtil.isEmpty(admSmsTemplateIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjAdmSmsReceiveRole> wrapper = new QueryWrapper<>();
        wrapper.select("adm_sms_template_id", "role_type")
                .in("adm_sms_template_id", admSmsTemplateIds);
        return this.list(wrapper);
    }

    // 根据后台短信模板ID获取接收角色类型
    @Override
    public List<ZjAdmSmsReceiveRole> getByAdmSmsTemplateId(Long admSmsTemplateId) {
        QueryWrapper<ZjAdmSmsReceiveRole> wrapper = new QueryWrapper<>();
        wrapper.select("adm_sms_template_id", "role_type")
                .eq("adm_sms_template_id", admSmsTemplateId);
        return this.list(wrapper);
    }
}
